package com.cars.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.cars.dao.util.ConectionManager;
import com.cars.dto.Product;

public class SearchInventoryDao {

	public List<Product> searchAllCars() {

		ResultSet resultSet = null;
		 
		Statement statement = null;
		List<Product> products = new ArrayList<Product>();
		Product product;
		try {
			// This will load the MySQL driver, each DB has its own driver
			Class.forName("com.mysql.jdbc.Driver");
			// Setup the connection with the DB

			// Statements allow to issue SQL queries to the database
			statement = ConectionManager.getInstance().getConnection()
					.createStatement();
			// Result set get the result of the SQL query
			resultSet = statement.executeQuery("select * from products");
			if (resultSet == null) {
				return null;
			}

			while (resultSet.next()) {
				product = new Product();
				product.setBuyPrice(resultSet.getDouble("buyPrice"));
				product.setMSRP(resultSet.getDouble("MSRP"));
				product.setProductCode(resultSet.getString("productCode"));
				product.setProductDescription(resultSet
						.getString("productDescription"));
				product.setProductLine(resultSet.getString("productLine"));
				product.setProductName(resultSet.getString("productName"));
				product.setProductScale(resultSet.getString("productScale"));
				product.setProductVendor(resultSet.getString("productVendor"));
				product.setQuantityInStock(resultSet.getInt("quantityInStock"));
				products.add(product);
			}

		} catch (Exception exception) {
			exception.printStackTrace();

		} finally {
			try {

				if (resultSet != null) {
					resultSet.close();
				}

				if (statement != null) {
					statement.close();

				}
			} catch (SQLException e) {
				e.printStackTrace();
			}

		}

		return products;

	}

}
